<?
/**
*  商品定单管理
*  2014/9/17
*/
include("database.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];
$table_name = "crm_order";
$dt = date("Y-m-d H:i:s");

if($action == "get_by_id") { 
	$sql = "select * from $table_name where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "del") {
	$db->doDelete($table_name, 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['mobile'])) {
		$condition .= " and mobile='".$_GET['mobile']."'";
	}
	if(isset($_GET['order_no'])) {
		$condition .= " and order_no='".$_GET['order_no']."'";
	}
	if(isset($_GET['order_status'])) {
		$condition .= " and order_status=".$_GET['order_status'];
	}
	$sql = "select id,product,total,type,price,box,truename,mobile, area, address, post_time, comment_score, order_source, content, order_status, '' as op_link from $table_name  where $condition order by id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("truename"=>"blue_color_mapper","product"=>"red_color_mapper", "op_link"=>"op_link_mapper"));

} else if($action == "get") {
	$sql = "select * from $table_name where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {
	$sql = "select id from crm_client where mobile='".$mobile."'";
	$user_id = $db->getOneValue($sql); // 查询手机是否存在
	if($user_id == null) {
		
		// 自动注册，首次下单的新用户
		$login_pass = rand(100000, 999999);
		$userinfo = array(
			"truename"=>$_POST["truename"],
			"mobile"=>$_POST["mobile"],
			"address"=>$_POST["address"],
			"area"=>$_POST["area"],
			"login_count"=>1,
			"order_count"=>1,
			"last_login"=>date("Y-m-d H:i:s"),
			"reg_time"=>date("Y-m-d H:i:s"),
			"login_pass"=>$login_pass
		);
		$db->doInsert("crm_client", $userinfo);
		$user_id = mysql_insert_id();
		$msg = iconv("utf-8", "gbk", $_POST["truename"])."，您好！首次购物后您已自动注册成为本站会员。登录账号：".$_POST['mobile']."，初始密码为：".$login_pass."，请妥善保存！【巨蟹座-长沙海鲜水产第一站】";
		file_get_contents("http://222.240.139.227/kangqi_helper/send_sms.jsp?mobile=".$_POST['mobile']."&content=$msg");
	}
	// 生成定单
	$_POST['user_id'] = $user_id;
	$_POST["order_no"] = microtime(true) * 100;
	$_POST["post_time"] = date("Y-m-d H:i:s");
	$db->doInsert("crm_order", $_POST);
	// 更新统计
	$db->query("update crm_client set order_count=order_count+1, total_money=total_money+".$_POST['total'].", 
	total_score=total_score+".  (intval($_POST['total'])*10)  ."  where id=".$user_id);
	echo 'ok';

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate($table_name, $_POST, 'id='.$id);
	echo 'ok';

}  else if($action == "check_order_message") {
	$new_order_count = $db->getOneValue("select count(0) as cnt from crm_order where order_status=1");
	$cancel_order_count = $db->getOneValue("select count(0) as cnt from crm_order where order_status=5");
	echo "{'new_order_count':$new_order_count,'cancel_order_count':$cancel_order_count}";

}  else if($action == "set_order_status") {
	$desc_arr = array("2"=>"确认备货", "3"=>"确认发货", "-1"=>"确认取消");
	$db->query("update crm_order set order_status=".$_GET['status'].", handle_time='$dt', handle_desc='".$desc_arr[$_GET['status']]."'  where id=".$_GET['id']);
	echo "ok";

}  else if($action == "cancel_order") {
	$db->query("update crm_order set order_status=5, cancel_reason=".$_POST['cancel_reason'].", 
	cancel_desc='".$_POST['cancel_desc']."', cancel_time='$dt', handle_time='$dt', handle_desc='400申请取消定单' where id=".$_GET['id']);
	echo "ok";

} else if($action == "do_callback") {
	file_get_contents("http://www.taobaobox.cn/quickcall/api/call.php?callid=".$_GET['id']."&telephone=".$_GET['mobile']."&taskid=45");
	echo "ok";

}

function op_link_mapper($v, $row) {
	$str = "";
	if($_SESSION['crm_uid'] == "shop" || $_SESSION['crm_user_flag']=="admin") {
		$str .= "<a href=\"javascript:set_order_status(".$row['id'].",2)\">备货</a>　<a href=\"javascript:set_order_status(".$row['id'].",3)\">发货</a>";
	}
	if($_SESSION['crm_uid'] == "400") {
		$str .= "　<a href=\"javascript:cancel_order(".$row['id'].")\">申请取消</a>";
	}
	if($_SESSION['crm_uid'] == "shop" || $_SESSION['crm_user_flag']=="admin") {
		$str .= "　<a href=\"javascript:set_order_status(".$row['id'].",-1)\">确认取消</a>";
	}
	if($_SESSION['crm_uid'] == "shop" || $_SESSION['crm_user_flag']=="admin") {
		$str .= "　<a href=\"javascript:do_callback(".$row['id'].", \\'".$row['truename']."\\', \\'".$row['mobile']."\\')\">回访</a>";
	}
	return $str;
}

?>